import { popup } from '@/js_sdk/wa-permission/native_popup';
import permisionUtil from '@/js_sdk/wa-permission/permission';
import useCommonStore from '@/store/common';

function gotoAppPermissionSetting(title = '权限说明', content = '当前功能权限已被您永久拒绝，请前往手机设置中打开') {
  uni.showModal({
    title,
    content,
    confirmText: '前往设置',
    success(e) {
      if (e.confirm) {
        permisionUtil.gotoAppPermissionSetting();
      }
    }
  });
}

export function chooseImage() {
  uni.addInterceptor('chooseImage', {
    invoke(args) {
      return new Promise(async (resolve) => {
        const perm_camera = uni.getStorageSync('android.permission.CAMERA');
        const perm_photo = uni.getStorageSync('android.permission.READ_EXTERNAL_STORAGE');

        if (perm_camera === 0 || perm_camera === '' || perm_photo === 0 || perm_photo === '') {
          popup.show({
            title: '权限申请说明',
            content: '为了修改个人头像和发布图片等, 我们需要申请您设备的相机和存储权限'
          });
        }

        let CAMERA = await permisionUtil.requestAndroidPermission('android.permission.CAMERA');
        let PHOTO = await permisionUtil.requestAndroidPermission('android.permission.READ_EXTERNAL_STORAGE');

        // if (CAMERA === -1 || PHOTO === -1) {
        //   gotoAppPermissionSetting();
        // }

        uni.setStorageSync('android.permission.CAMERA', CAMERA);
        uni.setStorageSync('android.permission.READ_EXTERNAL_STORAGE', PHOTO);
        resolve(args);
      });
    }
  });
}

export function saveImageToPhotosAlbum() {
  uni.addInterceptor('saveImageToPhotosAlbum', {
    invoke(args) {
      return new Promise(async (resolve) => {
        const perm_photo = uni.getStorageSync('android.permission.WRITE_EXTERNAL_STORAGE');

        if (perm_photo === 0 || perm_photo === '') {
          popup.show({
            title: '权限申请说明',
            content: '为了保存邀请码到手机相册, 我们需要申请您设备的存储权限'
          });
        }

        let PHOTO = await permisionUtil.requestAndroidPermission('android.permission.WRITE_EXTERNAL_STORAGE');

        // if (PHOTO === -1) {
        //   gotoAppPermissionSetting();
        // }

        uni.setStorageSync('android.permission.WRITE_EXTERNAL_STORAGE', PHOTO);
        resolve(args);
      });
    }
  });
}

export function scanCode() {
  uni.addInterceptor('scanCode', {
    invoke(args) {
      return new Promise(async (resolve) => {
        const perm_camera = uni.getStorageSync('android.permission.CAMERA');
        const perm_photo = uni.getStorageSync('android.permission.READ_EXTERNAL_STORAGE');

        if (perm_camera === 0 || perm_camera === '' || perm_photo === 0 || perm_photo === '') {
          popup.show({
            title: '权限申请说明',
            content: '为了识别二维码信息, 我们需要申请您设备的相机和存储权限'
          });
        }

        let CAMERA = await permisionUtil.requestAndroidPermission('android.permission.CAMERA');
        let PHOTO = await permisionUtil.requestAndroidPermission('android.permission.READ_EXTERNAL_STORAGE');

        // if (CAMERA === -1 || PHOTO === -1) {
        //   gotoAppPermissionSetting();
        // }

        uni.setStorageSync('android.permission.CAMERA', CAMERA);
        uni.setStorageSync('android.permission.READ_EXTERNAL_STORAGE', PHOTO);
        resolve(args);
      });
    }
  });
}

export function makePhoneCall() {
  uni.addInterceptor('makePhoneCall', {
    invoke(args) {
      return new Promise(async (resolve) => {
        const perm_phone = uni.getStorageSync('android.permission.CALL_PHONE');

        if (perm_phone === 0 || perm_phone === '') {
          popup.show({
            title: '权限申请说明',
            content: '为了联系客服/用户/咨询等, 我们需要申请您设备的拨打电话权限'
          });
        }

        let PHOTO = await permisionUtil.requestAndroidPermission('android.permission.CALL_PHONE');

        // if (PHOTO === -1) {
        //   gotoAppPermissionSetting();
        // }

        uni.setStorageSync('android.permission.CALL_PHONE', PHOTO);
        resolve(args);
      });
    }
  });
}

export function getLocation() {
  uni.addInterceptor('getLocation', {
    invoke(args) {
      return new Promise(async (resolve) => {
        const perm_phone = uni.getStorageSync('android.permission.ACCESS_FINE_LOCATION');

        if (perm_phone === 0 || perm_phone === '') {
          popup.show({
            title: '权限申请说明',
            content: '为了实现获取地理位置, 我们需要申请您设备的位置权限'
          });
        }

        let PHOTO = await permisionUtil.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION');

        // if (PHOTO === -1) {
        //   gotoAppPermissionSetting();
        // }

        uni.setStorageSync('android.permission.ACCESS_FINE_LOCATION', PHOTO);
        resolve(args);
      });
    }
  });
}

export function initInterceptor() {
  const commonStore = useCommonStore();
  if (commonStore.systemInfo.osName === 'ios') return;

  chooseImage();
  saveImageToPhotosAlbum();
  scanCode();
  makePhoneCall();
  getLocation();
}

export function judgePermissionPush() {
  const appAuthorizeSetting = uni.getAppAuthorizeSetting();

  const bol = appAuthorizeSetting.notificationAuthorized === 'authorized';

  if (!bol) {
    gotoAppPermissionSetting('权限说明', '通知权限已被您拒绝，请前往手机设置中打开');
  }
}
